let uuidList = [] //存放参会者的uuid
//存放当前摄像头设备的list
let vidoObj = {}
let videoLsit = []
// 存放当前麦克风设备的list
let audioObj = {}
let audioList = []

// 示例开始，以下几行代码即可成功入会专属云平台:
var inMeeting = false
window.onload = function () {
  getDemoConfig()
  window.zjsdk3 = new ZjSdk3();
  // 重写回调
  zjsdk3.on_dev_participant_update = demo_on_dev_participant_update;
  zjsdk3.on_dev_device_added = demo_on_dev_device_added
  zjsdk3.on_dev_call_succeed = demo_on_dev_call_succeed
}

// 参会人信息更新
function demo_on_dev_participant_update(pp, changed_keys) {
  uuidList.push(pp.uuid)
  sessionStorage.setItem('uuid', uuidList)
}

// 获取当前的本地设备
function demo_on_dev_device_added(device_id, label, kind, is_default) {
  //获取摄像头列表
  if (kind == 'videoinput') {
    vidoObj = {
      device_id: device_id, //设备id
      label: label //设备名称
    }
    videoLsit.push(vidoObj) //存放摄像头设备的list
    //获取摄像头设备列表
    let select = document.getElementById('videoDevices')
    //获取切换视频源的设备列表
    let selects = document.getElementById('overVideo')
    select.options.length = 0
    selects.options.length = 0
    videoLsit.forEach((item) => {
      select.options.add(new Option(item.label, item.device_id));
      selects.options.add(new Option(item.label, item.device_id));
    })
  } else if (kind == 'audioinput') {
    audioObj = {
      device_id: device_id, //设备id
      label: label //设备名称
    }
    audioList.push(audioObj) //存放麦克风设备的lsit
    // 获取麦克风设备列表
    let select = document.getElementById('audioDevices')
    //获取切换音频源的设备列表
    let selects = document.getElementById('overAudio')
    select.options.length = 0
    selects.options.length = 0
    audioList.forEach((item) => {
      select.options.add(new Option(item.label, item.device_id));
      selects.options.add(new Option(item.label, item.device_id));
    })
  }
}

// 呼叫成功时的回调
function demo_on_dev_call_succeed() {
  // 回显本地视频
  zjsdk3.local_dev_show_myvideo(['local_video'], true)
  // 显示远端视频
  zjsdk3.show_video_by_orders(['farend_video', 'farend_video2', 'farend_video3', 'farend_video4', 'farend_video5'])
  // 入会成功后打开当前默认的摄像头和麦克风
  let equipmentVideo = document.getElementById("videoDevices").value
  let equipmentAudio = document.getElementById("audioDevices").value
  zjsdk3.local_dev_switch_cam(equipmentVideo)
  zjsdk3.local_dev_switch_mic(equipmentAudio)
}


// 预览本地摄像头
window.preview = async function () {
  let videoValue = document.getElementById("videoDevices").value //当前摄像头设备变化的id
  let Videores = await zjsdk3.preview_dev_open_camera(videoValue)
  let localvideo = document.querySelector('#previewVideo') //存放预览摄像头视屏流的盒子
  if (localvideo) {
    localvideo.srcObject = Videores
    localvideo.play()
  }
}

// 呼入会议
window.start_call = async function () {
  if (!inMeeting) {
    inMeeting = true
    // 将用户输入的服务器地址、会议号、入会密码、昵称存入配置文件中
    setDemoConfig()
    await zjsdk3.make_call_now(demo_config.server.ip, demo_config.server.port, demo_config.meeting.cuid, demo_config.meeting.joinpwd, demo_config.meeting.nick_name, demo_config.meeting.account)
  }
};

// 离开会议室
window.disconnect = async function () {
  await zjsdk3.disconnect_now()
  window.inMeeting = false
  zjsdk3.preview_dev_finished()
  zjsdk3.local_dev_mute_cam(true)
  zjsdk3.local_dev_mute_mic(true)
};

//切换本地视频源
window.changeDevice = function () {
  var deviceId = document.getElementById("overVideo").value
  zjsdk3.local_dev_switch_cam(deviceId)
}
//切换本地音频源
window.changeMic = function () {
  var  deviceId= document.getElementById("overAudio").value
  zjsdk3.local_dev_switch_mic(deviceId)
}

//打开本地摄像头
window.Open_muteVideo = function () {
  var deviceId = document.getElementById("overVideo").value
  zjsdk3.local_dev_switch_cam(deviceId)
}
// 关闭本地摄像头
window.Close_muteVideo = function () {
  zjsdk3.local_dev_mute_cam(true)
}

// 打开本地麦克风
window.Open_muteAudio = function () {
  var deviceId = document.getElementById("overAudio").value
  zjsdk3.local_dev_switch_mic(deviceId)
}
//关闭本地麦克风
window.Close_muteAudio = function () {
  zjsdk3.local_dev_mute_mic(true)
}

// 指定观看视频流
window.force_get_streams = function () {
  if (sessionStorage.getItem('uuid')) {
    let uuid_list = sessionStorage.getItem('uuid').split(',')
    let layout = '1:' + (uuid_list.length - 1)
    zjsdk3.buffet_set_override_clayout(uuid_list, true, layout)
  }
}
// 使用平台推荐的视频流
window.cancel_force_get_streams = async function () {
  if (sessionStorage.getItem('uuid')) {
    let uuid_list = sessionStorage.getItem('uuid').split(',')
    let layout = '1:' + (uuid_list.length)
    zjsdk3.buffet_set_clayout(layout)
  }
}